<template>
  <div>
    <div class="bgfff mtrbl20 ptrbl20 nopb20">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="70px">
          <el-form-item prop="companyName">
            <el-input
              v-model="queryParams.companyName"
              placeholder="请输入所属企业名称"
              clearable
              size="medium"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
           <el-form-item>
              <el-button v-hasPermi="['offiaccount:middleground:save']" type="primary" plain icon="el-icon-plus" size="medium" @click="handleAdd">创建账号</el-button>
           </el-form-item>    
        </el-form>
        </el-col>
        </el-row>
      </div>
      <div class="app-container">  
        <el-row :gutter="20">
          <!--用户数据-->
          <el-col :span="24" :xs="24">
          <el-table v-loading="loading" :data="swmuserList">
          
          <el-table-column label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户姓名" align="left" key="nickName" prop="nickName"/>
          <el-table-column label="所属企业" align="left" key="companyName" prop="companyName"/>
          <el-table-column label="手机号码" align="left" key="phone" prop="phone"/>
          <el-table-column label="创建时间" align="left" key="createTime" prop="createTime"/>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <!-- <el-button size="small" type="text" icon="el-icon-view" @click="handleView(scope.row)">详情</el-button>
              <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> -->
              <el-dropdown size="mini" split-button type="primary" @click="handleView(scope.row)" placement="bottom-start" trigger="click">
                详 情
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-hasPermi="['offiaccount:middleground:update']" icon="el-icon-edit" @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item v-hasPermi="['offiaccount:middleground:delete']" icon="el-icon-delete" :disabled="scope.row.isDelete==true" @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户姓名" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户姓名" clearable :disabled="form.id != undefined"/>
            </el-form-item>
          </el-col>
         
          <el-col :span="24">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号码" clearable :disabled="form.id != undefined"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="客户企业" prop="companyId">
              <el-select v-model="form.companyId" filterable clearable placeholder="请选择客户企业" style="width: 100%;" @change="change" :disabled="form.id != undefined">
                <el-option
                  v-for="item in companyTreeSelect"
                  :key="item.id"
                  :label="item.companyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="isShow">
            <el-form-item label="公众号" prop="offiaccountIds">
               <el-select v-model="form.offiaccountIds" multiple filterable clearable placeholder="请选择公众号" style="width: 100%;">
                <el-option
                  v-for="(item,index) in offiaccountList"
                  :key="index"
                  :label="item.appName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


     <!-- 用户详情 -->
     <el-dialog title="用户详情" :visible.sync="openView" width="600px" append-to-body>
      <el-form label-width="150px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户姓名：">{{ swmuserinfo.nickName }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="手机号码：">{{ swmuserinfo.phone }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="客户企业：">{{ swmuserinfo.companyName }}</el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="创建时间：">{{ swmuserinfo.createTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- <el-form-item label="公众号：">
              <span v-for="(item,index) in swmuseroffiaccountList" :key="index" style="margin-right:10px;" >{{item.appName}}</span>
            </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>

<script>
import { swmuserList, getCompanyTreeSelect, offiaccountList, addSwmuser, updateSwmuser, delSwmuser, swmuserDetail } from "@/api/offiaccount/public";
export default {
  name: "Middleground",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      form1: {},
      // 是否显示详细弹出层
      openView: false,
      // 用户表格数据
      swmuserList: null,
      companyTreeSelect: [],
      offiaccountList: [],

      swmuserinfo :{},
      swmuseroffiaccountList:'',
      isShow:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        companyName: undefined
      },
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        phone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        companyId: [
          { required: true, message: "客户企业不能为空", trigger: "blur" }
        ],
        offiaccountIds: [
          { required: true, message: "公众号不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
    getCompanyTreeSelect().then(response => {
            this.companyTreeSelect = response.data;
      });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      swmuserList(this.queryParams).then(response => {
          this.swmuserList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickName: undefined,
        phone: undefined,
        companyId: undefined,
        offiaccountIds: []
      };
      this.resetForm("form");
      this.isShow = false;
    },
    handleAdd() {
      this.reset();
      // getUserOption().then(response => {
      //   //this.postOptions = response.posts;
      //   this.companyOptions = response.data.companies;
      //   this.roleOptions = response.data.roles;
        this.open = true;
        this.title = "创建账号";
      //   this.form.password = this.initPassword;
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id ;
      swmuserDetail({id:id}).then(response => {

        // offiaccountList(response.data.companyId).then(res => {
        //     this.offiaccountList = res.data
        // });
          //this.isShow = true;
        this.form = response.data.info;
        //this.companyTreeSelect = response.data.companyId;
        //alert(response.data.offiaccountList)
        this.offiaccountList = response.data.offiaccountList;
        this.isShow = true;
        this.open = true;
        this.title = "编辑账号";
        //});
      });
      //getUser(id).then(response => {
        // this.form = response.data.userInfo;
        // this.companyOptions = response.data.companies;
        // this.roleOptions = response.data.roles;
        //this.open = true;
        //this.title = "编辑账号";
      //});
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSwmuser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{});
          } else {
            addSwmuser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{});
          }
        }
      });
    },
    change(e){
      delete this.form.offiaccountIds
      if(e){
        offiaccountList(e).then(response => {
          this.isShow = true;
          this.offiaccountList = response.data.offiaccountList;
        }).catch(()=>{});
      }else{
        this.isShow = false
      }

      this.companyTreeSelect.map((item, i) => {
        if (item.id == e) {
          this.form.companyName = item.companyName; //传值
          //this.form.companyId = item.id;//传值
        }
      });

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
      
    },
    /** 人员信息详情 */
    handleView(row) {
      swmuserDetail({id:row.id}).then(response => {
        this.swmuserinfo = response.data.info;
        this.swmuseroffiaccountList = response.data.offiaccountList;
        this.openView = true;
      }).catch(()=>{});
    },
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除此用户?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSwmuser({id:id});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>
<style scoped lang="scss">
</style>